package com.stone.daoimpl;

import java.util.List;

import com.stone.dao.IStoneDao;
import com.stone.dao.hibernatebaseimpl.BaseDaoHibernateImpl;
import com.stone.model.Stone;
import com.stone.utils.Page;

public class StoneDao extends BaseDaoHibernateImpl<Stone, Long> implements IStoneDao {

	public Page findNewUploadStones4Page(int pageno, int pagesize) {
		String queryString = "from Stone stone order by stone.startTime desc";
		return this.findByQueryString4Page(queryString, new Object[]{}, pageno, pagesize);
	}

	public Page findNewUploadDisplayStones4Page(int pageno, int pagesize) {
		String queryString = "from Stone stone where stone.displayFlag is null or stone.displayFlag=1 order by stone.startTime desc";
		return this.findByQueryString4Page(queryString, new Object[]{}, pageno, pagesize);
	}
	
	public Page findVisitMostStones4Page(int pageno, int pagesize) {
		String queryString = "from Stone stone order by stone.visit desc";
		return this.findByQueryString4Page(queryString, new Object[]{}, pageno, pagesize);
	}

	public Page findVisitMostDisplayStones4Page(int pageno, int pagesize)
	{
		String queryString = "from Stone stone where stone.displayFlag is null or stone.displayFlag=1 order by stone.visit desc";
		return this.findByQueryString4Page(queryString, new Object[]{}, pageno, pagesize);
	}
	
	public Page searchStoneByNew4Page(Stone stone, int pageno, int pagesize) {
		String queryString = "from Stone stone where stone.material like ? and stone.color like ? and stone.minprice<? and stone.maxprice>? and stone.venation like ? and stone.purpose like ? and stone.chineseName like ? order by stone.startTime desc";
		Object[] values = new Object[]{stone.getMaterial(),stone.getColor(),stone.getMaxprice(),stone.getMinprice(),stone.getVenation(),stone.getPurpose(),"%"+stone.getChineseName()+"%"};
		return this.findByQueryString4Page(queryString, values, pageno, pagesize);
	}

	public Page searchStoneByVisit4Page(Stone stone, int pageno, int pagesize) {
		String queryString = "from Stone stone where stone.material like ? and stone.color like ? and stone.minprice<? and stone.maxprice>? and stone.venation like ? and stone.purpose like ? and stone.chineseName like ? order by stone.visit desc";
		Object[] values = new Object[]{stone.getMaterial(),stone.getColor(),stone.getMaxprice(),stone.getMinprice(),stone.getVenation(),stone.getPurpose(),"%"+stone.getChineseName()+"%"};
		return this.findByQueryString4Page(queryString, values, pageno, pagesize);
	}

	public Page findLikelyStones(Stone stone, int pageno, int size) {
		String queryString = "from Stone stone where stone.material = ? and stone.id != ?";
		Object[] values = new Object[]{stone.getMaterial(),stone.getId()};
		return this.findByQueryString4Page(queryString, values, pageno, size);
	}
	
	public Page searchCompanyByStone(Stone stone, int pageno, int pagesize) {
		String queryString = "from ProviderStone as model where model.stone.id=? order by model.weight desc";
		Object[] values = new Object[]{stone.getId()};
		return this.findByQueryString4Page(queryString, values, pageno, pagesize);
	}
	
	public Page getAllStone(int pageno, int pagesize) {
		String queryString = "from Stone stone order by stone.chineseName desc";
		return this.findByQueryString4Page(queryString, new Object[]{}, pageno, pagesize);
	}
	
	public Page getStonePageByName(String stoneName,int pageno, int pagesize) {
		String queryString = "from Stone stone where stone.chineseName like ? order by stone.chineseName desc";
		return this.findByQueryString4Page(queryString, new Object[]{"%"+stoneName+"%"}, pageno, pagesize);
	}

	public Page getStonePageByColor(String category, int pageno, int pagesize) {
		String queryString = "from Stone stone where stone.color like ?";
		return this.findByQueryString4Page(queryString, new Object[]{"%"+category+"%"}, pageno, pagesize);
	}

	public Page getStonePageByPurpose(String category, int pageno, int pagesize) {
		String queryString = "from Stone stone where stone.purpose like ?";
		return this.findByQueryString4Page(queryString, new Object[]{"%"+category+"%"}, pageno, pagesize);
	}
	
	
//	public Stone findById(java.lang.Long id) {
//		String queryString = "from Stone as stone where stone.id=?";
//		List list = getHibernateTemplate().find(queryString, id);
//		if(list.size()>0){
//			return (Stone) list.get(0);
//		}
//		else
//			return null;
//	}
	
}
